#include "sbox.h"

#define BITLENS 64

#ifdef __TEST__
#include <assert.h>
#endif


void LongBitsSbox(long_bits in, long_bits * out) {
  for(int i = 0; i < 4; i ++) {
    long_bit_block var8 = in.b4[i] ^ in.b0[i];
    long_bit_block var9 = var8 | in.b2[i];
    long_bit_block var10 = in.b6[i] ^ in.b2[i];
    long_bit_block var11 = var10 | in.b0[i];
    long_bit_block var12 = var11 & in.b4[i];
    long_bit_block var13 = in.b1[i] ^ var12;
    long_bit_block var14 = var13 | in.b7[i];
    long_bit_block var15 = var9 ^ var14;
    long_bit_block var16 = var9 & in.b7[i];
    long_bit_block var17 = var16 | in.b4[i];
    long_bit_block var18 = var9 & var15;
    long_bit_block var19 = var18 ^ var10;
    long_bit_block var20 = var19 & in.b0[i];
    long_bit_block var21 = var17 ^ var20;
    long_bit_block var22 = in.b0[i] & var21;
    long_bit_block var23 = var22 ^ in.b1[i];
    long_bit_block var24 = var23 | in.b6[i];
    long_bit_block var25 = var21 ^ var24;
    long_bit_block var26 = var25 & in.b3[i];
    long_bit_block var27 = var15 ^ var26;
    long_bit_block var28 = var21 | var11;
    long_bit_block var29 = var28 ^ in.b7[i];
    long_bit_block var30 = var29 | in.b4[i];
    long_bit_block var31 = in.b2[i] ^ var30;
    long_bit_block var32 = var15 | var20;
    long_bit_block var33 = var32 ^ var8;
    long_bit_block var34 = var33 | in.b6[i];
    long_bit_block var35 = var31 ^ var34;
    long_bit_block var36 = var26 ^ var10;
    long_bit_block var37 = var36 & in.b2[i];
    long_bit_block var38 = var13 & in.b7[i];
    long_bit_block var39 = var37 ^ var38;
    long_bit_block var40 = var39 & in.b3[i];
    long_bit_block var41 = var35 ^ var40;
    long_bit_block var42 = var41 & in.b5[i];
    long_bit_block var43 = var27 ^ var42;
    long_bit_block var44 = in.b2[i] & var41;
    long_bit_block var45 = var44 ^ var20;
    long_bit_block var46 = var20 | in.b6[i];
    long_bit_block var47 = var46 & var9;
    long_bit_block var48 = var47 & in.b5[i];
    long_bit_block var49 = var45 ^ var48;
    long_bit_block var50 = var46 ^ var19;
    long_bit_block var51 = var50 | var38;
    long_bit_block var52 = var38 ^ var32;
    long_bit_block var53 = var52 | var22;
    long_bit_block var54 = var53 & in.b5[i];
    long_bit_block var55 = var51 ^ var54;
    long_bit_block var56 = var55 & in.b4[i];
    long_bit_block var57 = var49 ^ var56;
    long_bit_block var58 = var39 | var33;
    long_bit_block var59 = var58 ^ in.b7[i];
    long_bit_block var60 = var45 | var25;
    long_bit_block var61 = var60 ^ var32;
    long_bit_block var62 = var61 | in.b5[i];
    long_bit_block var63 = var59 ^ var62;
    long_bit_block var64 = ~var53;
    long_bit_block var65 = var49 & in.b2[i];
    long_bit_block var66 = var64 ^ var65;
    long_bit_block var67 = var19 & var66;
    long_bit_block var68 = var67 ^ var24;
    long_bit_block var69 = var68 | in.b5[i];
    long_bit_block var70 = var66 ^ var69;
    long_bit_block var71 = var70 | in.b0[i];
    long_bit_block var72 = var63 ^ var71;
    long_bit_block var73 = var72 | in.b3[i];
    long_bit_block var74 = var57 ^ var73;
    long_bit_block var75 = var74 | in.b1[i];
    out->b4[i] = var43 ^ var75;
    long_bit_block var77 = var68 | in.b3[i];
    long_bit_block var78 = var77 ^ var11;
    long_bit_block var79 = var29 | in.b4[i];
    long_bit_block var80 = var79 ^ in.b3[i];
    long_bit_block var81 = var80 & in.b0[i];
    long_bit_block var82 = var78 ^ var81;
    long_bit_block var83 = var60 | var41;
    long_bit_block var84 = var83 ^ var23;
    long_bit_block var85 = var34 & var74;
    long_bit_block var86 = var85 | var64;
    long_bit_block var87 = var86 & in.b0[i];
    long_bit_block var88 = var84 ^ var87;
    long_bit_block var89 = var88 & in.b1[i];
    long_bit_block var90 = var82 ^ var89;
    long_bit_block var91 = var21 ^ in.b3[i];
    long_bit_block var92 = var91 & var84;
    long_bit_block var93 = var29 ^ var53;
    long_bit_block var94 = var93 & var85;
    long_bit_block var95 = var67 | var36;
    long_bit_block var96 = var95 ^ var56;
    long_bit_block var97 = var96 & in.b0[i];
    long_bit_block var98 = var94 ^ var97;
    long_bit_block var99 = var98 | in.b7[i];
    long_bit_block var100 = var92 ^ var99;
    long_bit_block var101 = var100 & in.b6[i];
    long_bit_block var102 = var90 ^ var101;
    long_bit_block var103 = var39 & var92;
    long_bit_block var104 = var103 ^ var86;
    long_bit_block var105 = var74 | var98;
    long_bit_block var106 = var105 ^ var56;
    long_bit_block var107 = var106 & in.b0[i];
    long_bit_block var108 = var104 ^ var107;
    long_bit_block var109 = var106 ^ var102;
    long_bit_block var110 = var109 & var13;
    long_bit_block var111 = var46 | in.b7[i];
    long_bit_block var112 = var110 ^ var111;
    long_bit_block var113 = var112 | in.b2[i];
    long_bit_block var114 = var108 ^ var113;
    long_bit_block var115 = var100 ^ var34;
    long_bit_block var116 = var115 | var102;
    long_bit_block var117 = in.b6[i] | in.b2[i];
    long_bit_block var118 = var116 ^ var117;
    long_bit_block var119 = var105 ^ var33;
    long_bit_block var120 = var119 ^ var12;
    long_bit_block var121 = var120 | in.b7[i];
    long_bit_block var122 = var20 ^ var121;
    long_bit_block var123 = var122 & in.b4[i];
    long_bit_block var124 = var118 ^ var123;
    long_bit_block var125 = var124 & in.b3[i];
    long_bit_block var126 = var114 ^ var125;
    long_bit_block var127 = var126 | in.b5[i];
    out->b2[i] = var102 ^ var127;
    long_bit_block var129 = var124 | out->b4[i];
    long_bit_block var130 = var129 ^ var25;
    long_bit_block var131 = var130 | in.b3[i];
    long_bit_block var132 = var84 ^ var131;
    long_bit_block var133 = var115 ^ var130;
    long_bit_block var134 = var133 | var98;
    long_bit_block var135 = var97 & in.b6[i];
    long_bit_block var136 = var134 ^ var135;
    long_bit_block var137 = var136 | in.b4[i];
    long_bit_block var138 = var132 ^ var137;
    long_bit_block var139 = ~in.b1[i];
    long_bit_block var140 = var92 ^ var85;
    long_bit_block var141 = var140 | var56;
    long_bit_block var142 = var141 | in.b2[i];
    long_bit_block var143 = var139 ^ var142;
    long_bit_block var144 = var13 | var103;
    long_bit_block var145 = var144 ^ var24;
    long_bit_block var146 = var145 | in.b3[i];
    long_bit_block var147 = var12 ^ var146;
    long_bit_block var148 = var147 | in.b6[i];
    long_bit_block var149 = var143 ^ var148;
    long_bit_block var150 = var149 & in.b5[i];
    long_bit_block var151 = var138 ^ var150;
    long_bit_block var152 = var61 & var147;
    long_bit_block var153 = var152 ^ var115;
    long_bit_block var154 = var150 & var70;
    long_bit_block var155 = var154 ^ var119;
    long_bit_block var156 = var155 & in.b1[i];
    long_bit_block var157 = var153 ^ var156;
    long_bit_block var158 = var56 ^ var138;
    long_bit_block var159 = var158 | var66;
    long_bit_block var160 = in.b2[i] | in.b3[i];
    long_bit_block var161 = var159 ^ var160;
    long_bit_block var162 = var161 & in.b0[i];
    long_bit_block var163 = var157 ^ var162;
    long_bit_block var164 = var15 ^ var73;
    long_bit_block var165 = var164 ^ in.b6[i];
    long_bit_block var166 = var99 ^ var121;
    long_bit_block var167 = var166 ^ var115;
    long_bit_block var168 = var140 ^ var23;
    long_bit_block var169 = var168 | var125;
    long_bit_block var170 = var169 & in.b0[i];
    long_bit_block var171 = var167 ^ var170;
    long_bit_block var172 = var171 & in.b4[i];
    long_bit_block var173 = var165 ^ var172;
    long_bit_block var174 = var173 | in.b5[i];
    long_bit_block var175 = var163 ^ var174;
    long_bit_block var176 = var175 | in.b7[i];
    out->b1[i] = var151 ^ var176;
    long_bit_block var178 = var64 ^ var126;
    long_bit_block var179 = var178 ^ var127;
    long_bit_block var180 = var96 ^ var60;
    long_bit_block var181 = var180 ^ var150;
    long_bit_block var182 = var181 & in.b0[i];
    long_bit_block var183 = var179 ^ var182;
    long_bit_block var184 = var69 & var136;
    long_bit_block var185 = var184 ^ var119;
    long_bit_block var186 = var185 & in.b1[i];
    long_bit_block var187 = var171 ^ var186;
    long_bit_block var188 = var187 & in.b7[i];
    long_bit_block var189 = var183 ^ var188;
    long_bit_block var190 = var72 & var116;
    long_bit_block var191 = var190 ^ var23;
    long_bit_block var192 = var191 | in.b7[i];
    long_bit_block var193 = var153 ^ var192;
    long_bit_block var194 = var159 & var167;
    long_bit_block var195 = var194 ^ var48;
    long_bit_block var196 = var82 | var195;
    long_bit_block var197 = var196 & var160;
    long_bit_block var198 = var197 & in.b0[i];
    long_bit_block var199 = var195 ^ var198;
    long_bit_block var200 = var199 | in.b1[i];
    long_bit_block var201 = var193 ^ var200;
    long_bit_block var202 = var201 | in.b4[i];
    long_bit_block var203 = var189 ^ var202;
    long_bit_block var204 = var70 ^ var146;
    long_bit_block var205 = var204 ^ var129;
    long_bit_block var206 = var164 | var23;
    long_bit_block var207 = var206 ^ var31;
    long_bit_block var208 = var207 & in.b0[i];
    long_bit_block var209 = var205 ^ var208;
    long_bit_block var210 = out->b4[i] & var190;
    long_bit_block var211 = var210 ^ var49;
    long_bit_block var212 = var211 & in.b5[i];
    long_bit_block var213 = var209 ^ var212;
    long_bit_block var214 = var184 & var176;
    long_bit_block var215 = var214 ^ var199;
    long_bit_block var216 = var151 | var108;
    long_bit_block var217 = var216 ^ var93;
    long_bit_block var218 = var217 & in.b7[i];
    long_bit_block var219 = var91 ^ var218;
    long_bit_block var220 = var219 | in.b1[i];
    long_bit_block var221 = var215 ^ var220;
    long_bit_block var222 = var221 & in.b3[i];
    long_bit_block var223 = var213 ^ var222;
    long_bit_block var224 = var223 | in.b2[i];
    out->b3[i] = var203 ^ var224;
    long_bit_block var226 = var27 | var57;
    long_bit_block var227 = var226 & var100;
    long_bit_block var228 = var43 ^ var70;
    long_bit_block var229 = var228 & var217;
    long_bit_block var230 = var176 & out->b4[i];
    long_bit_block var231 = var230 ^ var157;
    long_bit_block var232 = var231 & in.b0[i];
    long_bit_block var233 = var229 ^ var232;
    long_bit_block var234 = var233 | in.b4[i];
    long_bit_block var235 = var227 ^ var234;
    long_bit_block var236 = var115 & var167;
    long_bit_block var237 = var236 | var151;
    long_bit_block var238 = var237 & in.b5[i];
    long_bit_block var239 = var202 ^ var238;
    long_bit_block var240 = var9 & var51;
    long_bit_block var241 = var240 ^ var206;
    long_bit_block var242 = var53 | in.b5[i];
    long_bit_block var243 = var241 ^ var242;
    long_bit_block var244 = var243 & in.b7[i];
    long_bit_block var245 = var239 ^ var244;
    long_bit_block var246 = var245 | in.b3[i];
    long_bit_block var247 = var235 ^ var246;
    long_bit_block var248 = var70 & out->b2[i];
    long_bit_block var249 = var248 ^ var20;
    long_bit_block var250 = var88 & var173;
    long_bit_block var251 = var250 ^ var175;
    long_bit_block var252 = var196 & var52;
    long_bit_block var253 = var252 ^ var250;
    long_bit_block var254 = var253 & in.b0[i];
    long_bit_block var255 = var251 ^ var254;
    long_bit_block var256 = var255 | in.b6[i];
    long_bit_block var257 = var249 ^ var256;
    long_bit_block var258 = var35 ^ var193;
    long_bit_block var259 = var258 ^ var132;
    long_bit_block var260 = var147 & in.b6[i];
    long_bit_block var261 = var259 ^ var260;
    long_bit_block var262 = ~var214;
    long_bit_block var263 = var62 ^ var204;
    long_bit_block var264 = var263 | var260;
    long_bit_block var265 = var264 & in.b3[i];
    long_bit_block var266 = var262 ^ var265;
    long_bit_block var267 = var266 | in.b5[i];
    long_bit_block var268 = var261 ^ var267;
    long_bit_block var269 = var268 | in.b4[i];
    long_bit_block var270 = var257 ^ var269;
    long_bit_block var271 = var270 & in.b1[i];
    out->b0[i] = var247 ^ var271;
    long_bit_block var273 = var157 | var212;
    long_bit_block var274 = var273 ^ var226;
    long_bit_block var275 = var98 & var234;
    long_bit_block var276 = var275 | var253;
    long_bit_block var277 = var276 & in.b0[i];
    long_bit_block var278 = var274 ^ var277;
    long_bit_block var279 = var32 & var78;
    long_bit_block var280 = var279 ^ var235;
    long_bit_block var281 = var280 & in.b7[i];
    long_bit_block var282 = var278 ^ var281;
    long_bit_block var283 = var157 & var221;
    long_bit_block var284 = var283 ^ var44;
    long_bit_block var285 = var86 & in.b0[i];
    long_bit_block var286 = var284 ^ var285;
    long_bit_block var287 = var27 | var149;
    long_bit_block var288 = var287 ^ var57;
    long_bit_block var289 = var288 & in.b1[i];
    long_bit_block var290 = var286 ^ var289;
    long_bit_block var291 = var290 & in.b4[i];
    long_bit_block var292 = var282 ^ var291;
    long_bit_block var293 = var261 | var217;
    long_bit_block var294 = var293 ^ var249;
    long_bit_block var295 = var59 & var231;
    long_bit_block var296 = var295 ^ var142;
    long_bit_block var297 = var296 & in.b1[i];
    long_bit_block var298 = var294 ^ var297;
    long_bit_block var299 = var153 | var15;
    long_bit_block var300 = var299 & var164;
    long_bit_block var301 = var300 & in.b0[i];
    long_bit_block var302 = var298 ^ var301;
    long_bit_block var303 = var193 & var66;
    long_bit_block var304 = var303 ^ var120;
    long_bit_block var305 = var146 ^ var190;
    long_bit_block var306 = var305 | var304;
    long_bit_block var307 = var306 & in.b0[i];
    long_bit_block var308 = var304 ^ var307;
    long_bit_block var309 = var193 & var140;
    long_bit_block var310 = var309 | var286;
    long_bit_block var311 = var310 & in.b3[i];
    long_bit_block var312 = var308 ^ var311;
    long_bit_block var313 = var312 & in.b7[i];
    long_bit_block var314 = var302 ^ var313;
    long_bit_block var315 = var314 & in.b6[i];
    out->b5[i] = var292 ^ var315;
    long_bit_block var317 = var26 | var227;
    long_bit_block var318 = var317 ^ var308;
    long_bit_block var319 = var150 | var138;
    long_bit_block var320 = var319 ^ var116;
    long_bit_block var321 = var320 & in.b0[i];
    long_bit_block var322 = var318 ^ var321;
    long_bit_block var323 = out->b1[i] & var91;
    long_bit_block var324 = var323 | var292;
    long_bit_block var325 = var324 & in.b6[i];
    long_bit_block var326 = var322 ^ var325;
    long_bit_block var327 = var245 ^ var14;
    long_bit_block var328 = var327 | var219;
    long_bit_block var329 = var268 | var270;
    long_bit_block var330 = var329 ^ var253;
    long_bit_block var331 = var214 & in.b7[i];
    long_bit_block var332 = var330 ^ var331;
    long_bit_block var333 = var332 | in.b6[i];
    long_bit_block var334 = var328 ^ var333;
    long_bit_block var335 = var334 & in.b1[i];
    long_bit_block var336 = var326 ^ var335;
    long_bit_block var337 = var280 | var203;
    long_bit_block var338 = var337 ^ var155;
    long_bit_block var339 = var162 | in.b2[i];
    long_bit_block var340 = var338 ^ var339;
    long_bit_block var341 = var335 ^ var207;
    long_bit_block var342 = var341 ^ var227;
    long_bit_block var343 = var342 | in.b0[i];
    long_bit_block var344 = var141 ^ var343;
    long_bit_block var345 = var344 | in.b6[i];
    long_bit_block var346 = var340 ^ var345;
    long_bit_block var347 = var99 & var95;
    long_bit_block var348 = var347 ^ var44;
    long_bit_block var349 = var165 & in.b3[i];
    long_bit_block var350 = var348 ^ var349;
    long_bit_block var351 = var36 | var295;
    long_bit_block var352 = var351 ^ var178;
    long_bit_block var353 = var352 | in.b7[i];
    long_bit_block var354 = var72 ^ var353;
    long_bit_block var355 = var354 | in.b1[i];
    long_bit_block var356 = var350 ^ var355;
    long_bit_block var357 = var356 & in.b4[i];
    long_bit_block var358 = var346 ^ var357;
    long_bit_block var359 = var358 & in.b5[i];
    out->b7[i] = var336 ^ var359;
    long_bit_block var361 = ~var110;
    long_bit_block var362 = var361 | in.b3[i];
    long_bit_block var363 = var323 ^ var362;
    long_bit_block var364 = var108 & var90;
    long_bit_block var365 = var364 ^ var248;
    long_bit_block var366 = var365 & in.b5[i];
    long_bit_block var367 = var363 ^ var366;
    long_bit_block var368 = var264 ^ var101;
    long_bit_block var369 = var368 | var183;
    long_bit_block var370 = var41 & var207;
    long_bit_block var371 = var370 ^ var268;
    long_bit_block var372 = var341 | var187;
    long_bit_block var373 = var372 ^ var282;
    long_bit_block var374 = var373 & in.b0[i];
    long_bit_block var375 = var371 ^ var374;
    long_bit_block var376 = var375 | in.b5[i];
    long_bit_block var377 = var369 ^ var376;
    long_bit_block var378 = var377 & in.b7[i];
    long_bit_block var379 = var367 ^ var378;
    long_bit_block var380 = var200 ^ var118;
    long_bit_block var381 = var380 | var105;
    long_bit_block var382 = var169 | var179;
    long_bit_block var383 = var382 ^ var121;
    long_bit_block var384 = var194 | var83;
    long_bit_block var385 = var384 ^ var203;
    long_bit_block var386 = var385 & in.b0[i];
    long_bit_block var387 = var383 ^ var386;
    long_bit_block var388 = var387 | in.b5[i];
    long_bit_block var389 = var381 ^ var388;
    long_bit_block var390 = var252 ^ var352;
    long_bit_block var391 = var390 ^ var120;
    long_bit_block var392 = ~var130;
    long_bit_block var393 = var392 & in.b5[i];
    long_bit_block var394 = var391 ^ var393;
    long_bit_block var395 = var193 | var257;
    long_bit_block var396 = var395 ^ var252;
    long_bit_block var397 = var396 & in.b0[i];
    long_bit_block var398 = var394 ^ var397;
    long_bit_block var399 = var398 | in.b3[i];
    long_bit_block var400 = var389 ^ var399;
    long_bit_block var401 = var400 | in.b1[i];
    out->b6[i] = var379 ^ var401;
  }
}

int Block2LongBits(unsigned char *block, long_bits *out)
{
  /*block的大小在64位下是64字节，在32位下是32字节, 也就是block的大小和BITLENS相等 */
  unsigned char tmp;
  long_bit_block b0 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0, b7 = 0;
  for (int j = 0;j < 4 ; j ++) {
    for(int i = 0; i < BITLENS; i++) {
      tmp = block[j*64 + i];
      b0 = (b0 << 1) + ( tmp & 1);
      b1 = (b1 << 1) + ((tmp & 2) >> 1) ;
      b2 = (b2 << 1) + ((tmp & 4) >> 2);
      b3 = (b3 << 1) + ((tmp & 8) >> 3);
      b4 = (b4 << 1) + ((tmp & 16) >> 4);
      b5 = (b5 << 1) + ((tmp & 32) >> 5);
      b6 = (b6 << 1) + ((tmp & 64) >> 6);
      b7 = (b7 << 1) + ((tmp & 128) >> 7);
    }
    out->b0[j] = b0;
    out->b1[j] = b1;
    out->b2[j] = b2;
    out->b3[j] = b3;
    out->b4[j] = b4;
    out->b5[j] = b5;
    out->b6[j] = b6;
    out->b7[j] = b7;
  }
  
  return 0;
}

int LongBits2Block(long_bits in, unsigned char * out) {
  unsigned char tmp;
  long_bit_block mask;
  long_bit_block b0, b1, b2, b3, b4, b5, b6, b7;
  for(int k = 0,j = 0; k < 4; k ++) {
    b0 = in.b0[k];
    b1 = in.b1[k];
    b2 = in.b2[k];
    b3 = in.b3[k];
    b4 = in.b4[k];
    b5 = in.b5[k];
    b6 = in.b6[k];
    b7 = in.b7[k];
    for(int i = BITLENS - 1; i >= 0; i--, j ++) {
      tmp = 0;
      mask = (long_bit_block)1 << i;
      tmp += ((b7 & mask) >> i) << 7;
      tmp += ((b6 & mask) >> i) << 6; 
      tmp += ((b5 & mask) >> i) << 5;
      tmp += ((b4 & mask) >> i) << 4;
      tmp += ((b3 & mask) >> i) << 3;
      tmp += ((b2 & mask) >> i) << 2;
      tmp += ((b1 & mask) >> i) << 1;
      tmp += ((b0 & mask) >> i) << 0;
      out[j] = tmp;
    }
  }

  return 0;
}



#ifdef __TEST__
void test()
{



  unsigned char SM4_SBOX[256] = {
	0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
	0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
	0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,
	0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,
	0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,
	0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,
	0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,
	0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,
	0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,
	0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
	0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
	0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
	0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
	0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
	0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
	0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48
  };

  unsigned char block[257] = "this is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the testthis is the test\x00";

  long_bits plaintext = BUILDLONGBITS();

  // test for bit transform
  Block2LongBits(block, &plaintext);

  unsigned char buffer[257];
  memset(buffer, 0, 257);
  LongBits2Block(plaintext, buffer);
  assert(strcmp(block, buffer) == 0);


  //test for sbox
  long_bits cipher = BUILDLONGBITS();
  LongBitsSbox(plaintext, &cipher);
  memset(buffer, 0, 257);
  LongBits2Block(cipher, buffer);

  for (int i = 0; i < 256; i++) {
    block[i] = SM4_SBOX[block[i]];
  }
  assert(memcmp(block, buffer, 256) == 0);


}
#endif
